﻿// 功能：字母导航栏控件
// 描述：
// 作者：温伟鹏
// 日期：2010/5/26 21:53:27

using System;
using System.Runtime.InteropServices;
using MeizuSDK.Drawing;
using System.Drawing;

namespace MeizuSDK.Presentation
{
    /// <summary>
    /// 字母导航栏控件
    /// </summary>
    public class AlphabetBar : Control
    {
        #region DllImport

        [DllImport("MZFCWrapper.dll")]
        private static extern IntPtr UiAlphabetBarWrapperCtor(MouseEventFunc onLButtonDblClk, MouseEventFunc onLButtonDown,
        MouseEventFunc onLButtonUp, MouseEventFunc onMouseMove);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiAlphabetBarWrapperCalcIndexOfPosition(IntPtr pObj, int xPos, int yPos);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiAlphabetBarWrapperEnableZoomAlphabet(IntPtr pObj, bool bEnableZoomAlphabet);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiAlphabetBarWrapperGetCount(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiAlphabetBarWrapperGetCurIndex(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        [return: MarshalAs(UnmanagedType.LPTStr)]
        private static extern string UiAlphabetBarWrapperGetCurLetter(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiAlphabetBarWrapperGetZoomAlphabetOffset_X(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiAlphabetBarWrapperGetZoomAlphabetOffset_Y(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiAlphabetBarWrapperSetZoomAlphabetOffset_X(IntPtr pObj, int nXOffset);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiAlphabetBarWrapperSetZoomAlphabetOffset_Y(IntPtr pObj, int nYOffset);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiAlphabetBarWrapperInvalidate(IntPtr pObj, ref RECT prcUpdate);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool UiAlphabetBarWrapperIsEnableZoomAlphabet(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiAlphabetBarWrapperGetTextColor_Pressed(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiAlphabetBarWrapperSetTextColor_Pressed(IntPtr pObj, int clr);

        #endregion

        #region 私有变量

        private const string AlphabetTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        #endregion

        #region 事件
        /// <summary>
        /// 当用户选择字母后，触发此事件
        /// </summary>
        public event EventHandler CurrentLetterChanged;
        #endregion

        #region 公共属性
        /// <summary>
        /// 获取或设置一个值，指示点击控件上的某个字母后，是否放大显示在旁边的泡泡上. 
        /// </summary>
        public bool EnableZoom
        {
            get
            {
                return UiAlphabetBarWrapperIsEnableZoomAlphabet(Handle);
            }
            set
            {
                UiAlphabetBarWrapperEnableZoomAlphabet(Handle, value);
            }
        }
        /// <summary>
        /// 获得字母导航条中字母数量
        /// </summary>
        public int Count
        {
            get
            {
                return UiAlphabetBarWrapperGetCount(Handle);
            }
        }
        /// <summary>
        /// 获取当前点击的字母索引
        /// </summary>
        public int CurrentIndex
        {
            get{
                return UiAlphabetBarWrapperGetCurIndex(Handle);
            }
        }
        /// <summary>
        /// 获取当前点击的字母
        /// </summary>
        public string CurrentLetter
        {
            get
            {
                // +_+ 不知道为啥，调用UiAlphabet.GetCurLetter函数会报错 ╮(╯▽╰)╭

                char ch = AlphabetTable[CurrentIndex];

                return ch.ToString();
            }
        }
        /// <summary>
        /// 获取或设置放大的泡泡的位置X偏移
        /// </summary>
        public int OffsetX
        {
            get
            {
                return UiAlphabetBarWrapperGetZoomAlphabetOffset_X(Handle);
            }
            set
            {
                UiAlphabetBarWrapperSetZoomAlphabetOffset_X(Handle, value);
            }
        }
        /// <summary>
        /// 获取或设置放大的泡泡的位置Y偏移
        /// </summary>
        public int OffsetY
        {
            get
            {
                return UiAlphabetBarWrapperGetZoomAlphabetOffset_Y(Handle);
            }
            set
            {
                UiAlphabetBarWrapperSetZoomAlphabetOffset_Y(Handle, value);
            }
        }
        /// <summary>
        /// 获取或设置字母导航条按下后的字母颜色
        /// </summary>
        public Color PressedTextColor
        {
            get
            {
                int clr = UiAlphabetBarWrapperGetTextColor_Pressed(Handle);

                return MzDrawHelper.ConvertRgbToColor(clr);
            }
            set
            {
                int clr = MzDrawHelper.GetRGB(value);

                UiAlphabetBarWrapperSetTextColor_Pressed(Handle, clr);
            }
        }
        #endregion

        #region 构造函数

        public AlphabetBar()
        {
            Handle = UiAlphabetBarWrapperCtor(LButtonDbClkDelegate, LButtonDownDelegate, LButtonUpDelegate, MouseMoveDelegate);
            SetID();
            Width = 50;
        }
        #endregion

        #region 函数
        /// <summary>
        /// 引发 CurrentLetterChanged 事件
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnCurrentLetterChanged(EventArgs e)
        {
            if (CurrentLetterChanged != null)
            {
                CurrentLetterChanged(this, e);
            }
        }
        /// <summary>
        /// 重载OnMzCommand
        /// </summary>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        internal override void OnMzCommand(uint wParam, uint lParam)
        {
            int ctrId = (int)NativeMethods.LOWORD(wParam);

            if (ctrId == Id)
            {
                OnCurrentLetterChanged(EventArgs.Empty);
            }

            base.OnMzCommand(wParam, lParam);
        }
        #endregion
    }
}
